﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using mki.Backup.Objects.ValidationObjects;

namespace mki.Backup.Objects.Helper
{
	public static class ValidationExtension
	{
		#region Fields (1)

		// Fields (1) 

		private const String ruleSet = "DefaultRuleSet";

		#endregion Fields

		#region Methods (10)

		// Public Static Methods (9) 

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this BackupJobElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this DBBackupElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this RunProcessElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this FTPJobElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this FTPAddElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this FTPDeleteElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this ZipJobElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this ZipAddFileElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}

		/// <summary>
		/// Validates the specified source.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <returns></returns>
		public static ValidationError Validate(this ZipAddFolderElement source)
		{
			// create local fields
			var error = new ValidationError();
			var errorItems = new List<ValidationErrorItem>();

			// set source
			error.ReferenceItem = source;
			// BackupJob
			errorItems.AddRange(source.handleValidationResult(Validation.ValidateFromConfiguration(source, ruleSet)));

			// collect all and return
			error.Items = errorItems;
			error.IsValid = !(errorItems.Count > 0);
			return !error.IsValid ? error : null;

		}
		// Private Static Methods (1) 

		/// <summary>
		/// Fetches all errors on object
		/// </summary>
		/// <param name="source"> (Object) object which contains the errors </param>
		/// <param name="results"> (ValidationResults) result set of validation</param>
		/// <returns></returns>
		private static IEnumerable<ValidationErrorItem> handleValidationResult(this Object source, ValidationResults results)
		{
			var errorItems = new List<ValidationErrorItem>();

			if (!results.IsValid)
			{
				foreach (var result in results)
				{
					errorItems.Add(new ValidationErrorItem { Item = source, Field = result.Key ?? result.Message });
				}
			}

			return errorItems;
		}

		#endregion Methods
	}
}
